<?php

namespace app\xf\controller;

use app\xf\model\DataModel;
use app\xf\model\DataTypeModel;
use app\xf\model\FailepModel;
use app\xf\model\FileModel;
use app\xf\model\QuyuModel;
use app\xf\model\UserModel;
use think\Controller;
use think\Error;
use think\Request;

class DataController extends BaseAdminController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        //管理员获取全部的data
        if (self::$user['roles'] == 0) {
            $datas = DataModel::select()->toArray();

            foreach ($datas as $k => $v) {
                if ($v['type'] == 'polygon') {
                    if ($v['gdid'] != '' && count($v['gdid']) > 0) {
                        $datas[$k]['failps'] = FailepModel::where('gdid', $v['gdid'])->select()->toArray();
                    }
                }
            }
        } else {
            //判断区域数据
//
            if (self::$user['qyid'] != '') {

                $quyu = QuyuModel::find(self::$user['qyid']);

                $allquyu = QuyuModel::where('fid', 'like', $quyu->fid . "%")->select()->toArray();
                $dids = [];
                $datas = DataModel::select()->toArray();

                foreach ($allquyu as $v) {

                    foreach ($datas as $d) {

                        if (in_array($v['id'], $d['qyid'])) {

                            //在里面
                            array_push($dids, $d['id']);
                        }
                    }
                }
                $dids = array_unique($dids);
                $datas = [];
                //获取区域下所有的数据
                foreach ($dids as $v) {
                    $data = DataModel::find($v);
//                   
                    if ($data) {
                        //判断是否管道
                        if ($data['type'] == 'polygon') {
                            if ($data['gdid'] != '' && count($data['gdid']) > 0) {
                                $data['failps'] = FailepModel::where('gdid', $data['gdid'])->select()->toArray();
                            }
                        }
                        array_push($datas, $data);
                    }
                }

                return $this->SuccessReturn('ok', $datas);

//                $datas = [];
//                foreach ($users as $v) {
//                    $data = DataModel::where('uid', $v['id'])->select()->toArray();
//
//                    if (count($data) > 0) {
//                        foreach ($data as $v) {
//                            array_push($datas, $v);
//                        }
//                    }
//                }
            }


        }

        return $this->SuccessReturn('ok', $datas);
    }

    public function getAllFile($fid = 0)
    {
        $files = FileModel::where('fid', $fid)
            ->with("data")
            ->where("uid", self::$user['id'])
            ->select()->toArray();

        if (count($files) > 0) {
            foreach ($files as $v) {
                //获取下级
                $res = FileModel::where('fid', $v['id'])
                    ->where('uid', self::$user['id'])
                    ->find();
                if ($res) {
                    $v['children'] = $this->getAllFile($v['id']);
                }
            }
            return $files;
        } else {
            return $files;
        }
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        $data = $request->post('data');
        //获取参数
        if ($data == '') return $this->ErrorReturn(403, "缺少必要参数");
        $res_data = [];
//        dump($data);
//        return '';
        if (is_array($data)) {
            foreach ($data as $v) {
                $name = array_key_exists('name', $v) ? $v['name'] : "未定义标注";
                $fileid = array_key_exists('fileid', $v) ? $v['fileid'] : 0;
                $type = array_key_exists('type', $v) ? $v['type'] : "";
                $latlng = array_key_exists('latlng', $v) ? $v['latlng'] : "";
                $config = array_key_exists('config', $v) ? $v['config'] : '';
                $did = array_key_exists('did', $v) ? $v['did'] : '';
                $gdid = array_key_exists('gdid', $v) ? $v['gdid'] : '';
                $style = array_key_exists('style', $v) ? $v['style'] : [];
                $qyid = array_key_exists('qyid', $v) ? $v['qyid'] : [];
                $iconid = array_key_exists('iconid', $v) ? $v['iconid'] : '';
                if ($type != "" && $latlng != '') {
                    //存储数据
                    //检查latlng 是否是标准的
                    $new_data = new DataModel();
                    $new_data->name = $name;
                    $new_data->fileid = $fileid;
                    $new_data->type = $type;
                    $new_data->qyid = $qyid;
                    $new_data->did = $did;
                    $new_data->style = $style;
                    $new_data->config = $config;
                    $new_data->gdid = $gdid;
                    $new_data->iconid = $iconid;
                    switch ($type) {
                        case 'point':
                            if (is_array($latlng)) {
                                $lat = (float)$latlng[1];
                                $lng = (float)$latlng[0];
                                if (-90 < $lat && $lat < 90 && -180 < $lng && $lng < 180) {
                                    //开始存储
                                    $new_data->latlng = [$lng, $lat];
                                }
                            }
                            break;
                        default :
                            if (is_array($latlng)) {

                                $line_latlng = [];

                                for ($i = 0; $i < count($latlng); $i += 2) {
                                    $lat = (float)$latlng[$i + 1];
                                    $lng = (float)$latlng[$i];
                                    if (-90 < $lat && $lat < 90 && -180 < $lng && $lng < 180) {
                                        array_push($line_latlng, $lng);
                                        array_push($line_latlng, $lat);
                                    }
                                }
                                $new_data->latlng = $line_latlng;
                            }
                            break;
                    }
                    $new_data->save();
                    $new_data['icon'] = $new_data->icon;
                    array_push($res_data, $new_data);
                }
            }
        }


//        if ($name == '' || $type == '' || $latlng == '') {
//            return $this->ErrorReturn(403, '缺少参数', $data);
//        }

        return $this->SuccessReturn('添加成功', $res_data);
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        //
        $data = $request->put();
        //获取参数
        $id = array_key_exists('id', $data) ? $data['id'] : '';
        $name = array_key_exists('name', $data) ? $data['name'] : '';
        $type = array_key_exists('type', $data) ? $data['type'] : "";
        $fileid = array_key_exists('fileid', $data) ? $data['fileid'] : '无';
        $latlng = array_key_exists('latlng', $data) ? $data['latlng'] : "";
        $config = array_key_exists('config', $data) ? $data['config'] : '';
        $gdid = array_key_exists('gdid', $data) ? $data['gdid'] : '';
        $qyid = array_key_exists('qyid', $data) ? $data['qyid'] : '';
        $did = array_key_exists('did', $data) ? $data['did'] : '';
        $iconid = array_key_exists('iconid', $data) ? $data['iconid'] : '';
        $style = array_key_exists('style', $data) ? $data['style'] : '';
        if ($id == '') return $this->ErrorReturn(403, '缺少必要参数', $data);
        $id = ltrim($id, '000');


        //查询修改的对象
        $data_res = DataModel::find($id);
        if (!$data_res) return $this->ErrorReturn(400, '查询不到对象');
        if ($name != '') $data_res->name = $name;
        if ($type != '') $data_res->type = $type;
        if ($fileid != '无') $data_res->fileid = $fileid;
        if ($config != '') $data_res->config = $config;
        if ($qyid != '') $data_res->qyid = $qyid;
        if ($gdid != '') $data_res->gdid = $gdid;
        if ($did != '') $data_res->did = $did;
        if ($style != '') $data_res->style = $style;
        if ($iconid != '') $data_res->iconid = $iconid;
        if ($latlng != '') {
            //检查latlng 是否正确
            switch ($type) {
                case 'point':
                    if (is_array($latlng)) {
                        $lat = (float)$latlng[1];
                        $lng = (float)$latlng[0];
                        if (-90 < $lat && $lat < 90 && -180 < $lng && $lng < 180) {
                            //开始存储
                            $data_res->latlng = [$lng, $lat];
                        }
                    }
                    break;
                default :
                    if (is_array($latlng)) {
                        $line_latlng = [];

                        for ($i = 0; $i < count($latlng); $i += 2) {
                            $lng = (float)$latlng[$i];
                            $lat = (float)$latlng[$i + 1];
                            if (-90 < $lat && $lat < 90 && -180 < $lng && $lng < 180) {
                                array_push($line_latlng, $lng);
                                array_push($line_latlng, $lat);
                            }
                        }
                        $data_res->latlng = $line_latlng;
                    }
                    break;
            }
        }
        $data_res['icon'] = $data_res->icon;
        $res = $data_res->save();
        if ($res) return $this->SuccessReturn('修改成功', $data_res);
        return $this->ErrorReturn(400, '修改失败');
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id, Request $request)
    {
        //
        $id = $request->post('id');
        if ($id == 0) return $this->ErrorReturn(400, '数据id无效');
        //查询是否存在此用户
        $data = DataModel::where('id', $id)->find();
        if (!$data) return $this->ErrorReturn(404, '找不到此区域');
        $data->delete();
        return $this->SuccessReturn('删除成功');
    }


    public function delAllData(Request $request)
    {
        $ids = $request->post('ids');

        if (!is_array($ids)) {
            return $this->ErrorReturn(400, '没获取到数组');
        }

        foreach ($ids as $v) {
            $data = DataModel::find($v);
            if ($data) $data->delete();
        }
        return $this->SuccessReturn('删除成功');

    }
}
